<template>
  <div style="margin-top: 10%">
    <el-container>
      <el-header>软件技术超市欢迎您！</el-header>

      <el-main>
        <div class="container">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="用户名" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >登录</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-main>

      <el-footer>version: 1.0.0 &nbsp; &nbsp; copyright@jiangwenhuan</el-footer>
    </el-container>
  </div>
</template>




<script>
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        name: "",
        password: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        const { name, password } = this.ruleForm; //从表单页面里获取数据
        const data = {
          username: name,
          password: password,
        }; //
        //
        //
        //
        //
        //
        if (valid) {
          //把用户名和密码发送到后端进行匹配验证
          this.$axios
            .post("http://mock-api.com/VKywG3gw.mock/login", data)
            .then((res) => {
              // alert(res.data.code)
              // alert(res.data.message)
              if (res.data.code == 200) {
                sessionStorage.setItem("username", name);
                this.$router.replace("/home");
              } else {
                alert("登录失败！");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style scoped>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}
.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}
.container {
  width: 300px;
  margin: auto;
}
</style>